<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/helper/Abstract.php");

class PHP_CMS_View_Helper_Index extends PHP_CMS_View_Helper_Abstract {


    private $bean;
    private $filters;
    private $form;
    private $action;
    private $actions = array();
    private $addnew;


    // ----------------------------------------------------------- Constructors


    public function index() {
        return $this;
    }


    // ------------------------------------------------------ Protected Methods


    protected function init() {
        $this->bean->setForm($this->form);
        $this->rows = $this->bean->getRows();
    }


    // ---------------------------------------------------- HTML output methods


    public function start() {

        $this->init();

    ?>

    <form id="<?=get_class($this->form)?>"
          method="post"
          action="<?=$this->action?>"
          onsubmit="return confirmDelete('<?=$this->message("message.confirm.delete", _Core::BUNDLE)?>', this)">


        <? /* Show the Search & Filter Panel */ ?>
        <?=$this->panel()?>


        <? /* Start output buffering in case there are no results */ ?>
        <?
//            ob_start();
        ?>

    <?

    }


    public function end() {

        if($this->rows == 0) {

//            ob_end_clean();
            ?>
            <div id="errorMsg">
            <h3><?=$this->message("list.outputbuffer.header", _Core::BUNDLE)?></h3>
            <ol>
            <? if(!is_null($this->addnew)): ?>
                <li><a href="<?=$this->addnew?>"><?=$this->message("list.outputbuffer.new", _Core::BUNDLE)?>.</a></li>
            <? endif; ?>
            <li><a href="javascript:query('<?=get_class($this->form)?>')"><?=$this->message("list.outputbuffer.adjust", _Core::BUNDLE)?></a></li>
            <li><a href="javascript:clean('<?=get_class($this->form)?>')"><?=$this->message("list.outputbuffer.clear", _Core::BUNDLE)?></a></li>
            </ol>
            </div>
            <?

        } else {

//            ob_get_flush();

            ?>

            <table cellspacing="0" cellpadding="0" class="list">
            <tr>

                <?
                $count = 0;
                foreach($this->headers as $name => $value):

                    $class = "";
                    if($count++ == 0) {
                        $class = " class=\"checkbox\"";
                    } else if($value[1] === false) {
                        $class = " class=\"narrow\"";
                    }

                    ?>

                    <th<?=$class?>>
                        <?=$value[0] !== null ? $this->th($value[0], $name) : $name?>
                    </th>

                <? endforeach; ?>

            </tr>

            <?=$this->sb?>

            </table>
            
            <?
            if(count($this->actions) > 0)
                $this->edit();

        }

        ?>
        </form>
        <?
    }


    function th($field, $header) {

        // what image are we showing.
        $image = "cms/core/view/gfx/". ($this->form->getOrder() == $field ? "bullet_arrow_" . ($this->form->getDirection() == "asc" ? "up" : "down") .".png" : "void.gif");

?><a href="javascript:setOrder('<?=get_class($this->form)?>','<?=$field?>')"><?=$header?></a>
<img src="static.php?file=<?=$this->image($image)?>" width="16" height="5" alt="" class="sort" />

<?
    }


    protected function edit() {
        ?>
        <div class="ctrl" style="padding-left:2px">

            <img src="static.php?file=<?=$this->image("cms/core/view/gfx/arrow_ltr.png")?>" width="38" height="22" alt="" style="vertical-align:-7px" />
            <a href="javascript:checkAll('<?=get_class($this->form)?>')"><?=$this->pageContext->message("choose.check-all", _Core::BUNDLE)?></a> /
            <a href="javascript:uncheckAll('<?=get_class($this->form)?>')"><?=$this->pageContext->message("choose.uncheck-all", _Core::BUNDLE)?></a>

            &nbsp;|&nbsp;

            <select name="actions" onchange="this.form.action.value=this.options[this.selectedIndex].value">
                <option value=""><?=$this->pageContext->message("choose.with-selected", _Core::BUNDLE)?>:</option>
                <? foreach(array_keys($this->actions) as $key) { ?>
                    <option value="<?=$key?>" <?=$this->form->getAction() == $key ? " selected=\"selected\"" : ""?>><?=$this->actions[$key]?></option>
                <? } ?>
            </select>

            &nbsp;|&nbsp;

            <input class="button" type="submit" value="<?=$this->pageContext->message("button.go", _Core::BUNDLE)?>" />

        </div>
        <?
    }


    protected function panel() {

        // Save a token
        require_once("mvc/util/TokenProcessor.php");
        $this->addnewken = TokenProcessor::getInstance();
        $this->addnewken->saveToken($this->pageContext->request);

?>
    <div>
        <input type="hidden" name="order" value="<?=$this->form->getOrder()?>" />
        <input type="hidden" name="direction" value="<?=$this->form->getDirection()?>" />
        <input type="hidden" name="page" value="<?=$this->form->getPage()?>" />
        <input type="hidden" name="id" value="<?=$this->form->getId()?>" />
        <input type="hidden" name="action" value="<?=$this->form->getAction()?>" />
        <input type="hidden" name="<?=MVC::TOKEN_KEY?>" value="<?=$this->pageContext->request->getSession()->getAttribute(MVC::TRANSACTION_TOKEN_KEY)?>" />
    </div>

    <div class="ctrl" style="line-height:25px;">

        <? if(!is_null($this->addnew)) { ?>
            <input class="button" type="button" name="new"
                   value="<?=$this->message("button.new", _Core::BUNDLE)?>"
                   onclick="to('<?=$this->addnew?>')"
                   title="<?=$this->message("title.new", _Core::BUNDLE)?>" /> &nbsp;|&nbsp;
        <? } ?>


        <?=$this->message("label.search", _Core::BUNDLE)?> :
        <input class="panel" type="text" name="query" value="<?=$this->form->getQuery()?>" onkeydown="this.form.page.value=1" title="<?=$this->message("title.search", _Core::BUNDLE)?>" />

        &nbsp;|&nbsp;

        <?=$this->message("label.fields", _Core::BUNDLE)?> :
        <select name="filter" onchange="this.form.page.value=1" title="<?=$this->message("title.filter.fields", _Core::BUNDLE)?>" class="panel">
            <option value=""><?=$this->message("choose.all", _Core::BUNDLE)?></option>
            <? foreach(array_keys($this->filters) as $key) { ?>
            <option value="<?=$key?>" <?=$this->form->getFilter() == $key ? " selected=\"selected\"" : ""?>><?=$this->filters[$key]?></option>
            <? } ?>
        </select>

        &nbsp;|&nbsp;

        <? if($this->languages && $this->param(_Core::I18N)) { ?>

            <?=$this->pageContext->message("label.languages", _Core::BUNDLE)?>:
            <select name="language" onchange="this.form.page.value=1" title="<?=$this->message("title.filter.language", _Core::BUNDLE)?>"  class="panel">
                <option value=""<?=$this->form->getLanguage() == "" ? " selected=\"selected\"" : ""?>><?=$this->message("choose.all", _Core::BUNDLE)?></option>
                <? foreach(LanguagePeer::getI18n() as $language): ?>
                    <option value="<?=$language->getCode()?>"<?=$this->form->getLanguage() == $language->getCode() ? " selected=\"selected\"" : ""?>><?=$language->getName()?></option>
                <? endforeach; ?>
            </select>

            &nbsp;|&nbsp;

        <? } ?>

        <?=$this->pageContext->message("label.results-per-page", _Core::BUNDLE)?>:
        <select name="results" onchange="this.form.page.value=1" title="<?=$this->message("title.filter.results", _Core::BUNDLE)?>">
            <option value="10"<?=$this->form->getResults() == "10" ? " selected=\"selected\"" : ""?>>10</option>
            <option value="20"<?=$this->form->getResults() == "20" ? " selected=\"selected\"" : ""?>>20</option>
            <option value="30"<?=$this->form->getResults() == "30" ? " selected=\"selected\"" : ""?>>30</option>
            <option value="40"<?=$this->form->getResults() == "40" ? " selected=\"selected\"" : ""?>>40</option>
            <option value="50"<?=$this->form->getResults() == "50" ? " selected=\"selected\"" : ""?>>50</option>
            <option value="100"<?=$this->form->getResults() == "100" ? " selected=\"selected\"" : ""?>>100</option>
            <option value=""<?=$this->form->getResults() == "" ? " selected=\"selected\"" : ""?>><?=$this->message("choose.all", _Core::BUNDLE)?></option>
        </select>

        &nbsp;|&nbsp;

        <input type="button" value="<?=$this->message("button.clear", _Core::BUNDLE)?>"
               class="button" onclick="clean('<?=get_class($this->form)?>')"
               title="<?=$this->message("title.filter.clear", _Core::BUNDLE)?>" />

        &nbsp;|&nbsp;

        <input type="submit" value="<?=$this->message("button.search", _Core::BUNDLE)?>" class="button" />

    </div><?

    }


    // --------------------------------------------------------- Result methods


    private $sb = "";
    private $data;
    private $buttons;
    private $headers = array();


    public function startRow() {
        $this->data = array();
        $this->buttons = array();
    }

    public function addData($value, $title=null) {
        $this->data[] = "<td".
            ($title !== null ? " title=\"".htmlentities($title)."\"" : "").
            (count($this->data) == 0 ? " class=\"checkbox\"" : "").">".$value."</td>";
    }

    public function addButton($value, $onclick) {
        $this->buttons[] = "<input type=\"button\" ".
                               "onclick=\"".$onclick."\" ".
                               "class=\"button\" ".
                               "value=\"".$value."\" />";
    }

    public function addCheckbox($value) {
        $this->addData("<input type=\"checkbox\" ".
                        "name=\"items[]\" ".
                        "value=\"".$value."\" ".
                        (in_array($value, $this->form->getItems()) ? " checked=\"checked\"" : "")." />");
    }

    public function endRow() {

        $this->sb .= "<tr class=\"ctrl\">";

        foreach($this->data as $data) {
            $this->sb .= $data;
        }

        $this->sb .= "<td class=\"buttons\">";

        foreach($this->buttons as $button) {
            $this->sb .= $button;
        }

        $this->sb .= "</td>";

        $this->sb .= "</tr>";
    }


    // ------------------------------------------------------------- Properties


    public function setActionForm($f) {
        $this->form = $f;
        return $this;
    }

    public function setAction($a) {
        $this->action = $a;
        return $this;
    }

    public function setActions($a) {
        $this->actions = $a;
        return $this;
    }

    public function setAddNewURL($a) {
        $this->addnew = $a;
        return $this;
    }

    public function setBean($b) {
        $this->bean = $b;
        return $this;
    }

    public function setFilterFields($f) {
        $this->filters = $f;
        return $this;
    }

    public function addHeader($name, $sort=null, $wide=false) {
        $this->headers[$name] = array($sort, $wide);
        return $this;
    }


    // -------------------------------------------------------- Private Methods


}

?>